﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SharepointSolutionInstaller.Utils.SolutionDeployment
{
    public class SiteCollectionInfo
    {
        private readonly SPSite siteCollection;
        private readonly bool required;

        internal SiteCollectionInfo(SPSite siteCollection, bool required)
        {
            this.siteCollection = siteCollection;
            this.required = required;
        }

        internal SPSite SiteCollection
        {
            get { return siteCollection; }
        }

        internal bool Required
        {
            get { return required; }
        }

        public override string ToString()
        {
            string rootWebTitle = String.Empty;
            try
            {
                // Note that this does involve doing an OpenWeb on the root web
                // which does have a performance penalty
                // The thought is that the information is worth the performance cost
                rootWebTitle = siteCollection.RootWeb.Title;
            }
            catch (UnauthorizedAccessException)
            {
                rootWebTitle = "<cannot access root web - access denied>";
            }
            catch (Exception ex)
            {
                rootWebTitle = "<" + ex.Message + ">";
            }
            return siteCollection.Url + "    (" + rootWebTitle + ")";
        }
    }
}
